package com.gesture;

import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.DrawableContainer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ViewFlipper;

import com.alarm.R;
import com.notification.NotificationSetting;

public class SetGesture extends Activity{
	final String TAG = "SetGesture";
	NotificationSetting setting;
	Button btnDone = null;
	ViewFlipper[] vfs = new ViewFlipper[4];
	ListView[] lvGesture = new ListView[4];
	int[] gestureId = new int[4];
	
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.set_gesture);
		bundle = this.getIntent().getExtras();
		setting = (NotificationSetting) bundle.get("setting");
		btnDone = (Button)findViewById(R.id.btnDone);
		vfs[0] = (ViewFlipper)findViewById(R.id.vf0);
		vfs[1] = (ViewFlipper)findViewById(R.id.vf1);
		vfs[2] = (ViewFlipper)findViewById(R.id.vf2);
		vfs[3] = (ViewFlipper)findViewById(R.id.vf3);
		for(int i=0;i<vfs.length;i++){
			if (setting.getGesture().length>i)
				vfs[i].setDisplayedChild(setting.getGesture()[i]);
			else
				vfs[i].setDisplayedChild(0);
		}
		refresh();
	}
	
	public void onImgClick(View v){
		((ViewFlipper) v.getParent()).showNext();
		refresh();
	}
	
	private void refresh(){
		for(int i=1;i<vfs.length;i++)
			if (vfs[i-1].getCurrentView().getId()==R.id.bar){
				vfs[i].setVisibility(ViewFlipper.INVISIBLE);
				for(int j=i+1;j<vfs.length;j++){
					vfs[j].setVisibility(ViewFlipper.INVISIBLE);
				}
				break;
			}else {
				vfs[i].setVisibility(ViewFlipper.VISIBLE);
			}
	}
	
	public void onCancel(View v){
		setResult(Activity.RESULT_CANCELED);
		finish();
	}
	public void onGestureDoneClick(View v){
		for (int i=0;i<gestureId.length;i++){
			gestureId[i] = vfs[i].getDisplayedChild();
		}
		setting.setGesture(gestureId);
		if (!setting.existGesture())
			setting.setGestureOn(false);
		setting.save();
		Intent returnSetting = new Intent();
		Bundle bundle = new Bundle();
		bundle.putSerializable("setting", setting);
		returnSetting.putExtras(bundle);
		setResult(Activity.RESULT_OK, returnSetting);
		finish();
	}
}
